package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.button;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.RoleMenuButtonMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.MenuButton;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.RoleMenuButton;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.button.JsmRoleMenuButtonService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 卢曼成
 * @Description 角色资源按钮逻辑层
 * @Date 2017/2/20 17:20
 */

public class JsmRoleMenuButtonServiceImpl implements JsmRoleMenuButtonService {
    @Autowired
    private RoleMenuButtonMapper roleMenuButtonMapper;

    /**
     * 根据ID删除
     *
     * @param roleMenuButtonId
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String roleMenuButtonId) {
        return roleMenuButtonMapper.deleteByPrimaryKey(roleMenuButtonId);
    }

    /**
     * 添加
     *
     * @param record
     * @return
     */
    @Override
    public int insert(RoleMenuButton record) {
        return roleMenuButtonMapper.insert(record);
    }

    /**
     * 非空添加
     *
     * @param record
     * @return
     */
    @Override
    public int insertSelective(RoleMenuButton record) {
        return roleMenuButtonMapper.insertSelective(record);
    }

    /**
     * 根据ID查询
     *
     * @param roleMenuButtonId
     * @return
     */
    @Override
    public RoleMenuButton selectByPrimaryKey(String roleMenuButtonId) {
        return roleMenuButtonMapper.selectByPrimaryKey(roleMenuButtonId);
    }

    /**
     * 根据条件查询ID集合
     *
     * @param list
     * @return
     */
    @Override
    public List<String> selectByListId(List<RoleMenuButton> list) {
        return roleMenuButtonMapper.selectByListId(list);
    }

    /**
     * 非空修改
     *
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(RoleMenuButton record) {
        return roleMenuButtonMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 修改
     *
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKey(RoleMenuButton record) {
        return roleMenuButtonMapper.updateByPrimaryKey(record);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量添加
     * @Date 2017/2/8 17:17
     */
    @Override
    public int insertList(List<RoleMenuButton> list) {
        return roleMenuButtonMapper.insertList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量修改
     * @Date 2017/2/8 17:17
     */
    @Override
    public int updateList(List<RoleMenuButton> list) {
        return roleMenuButtonMapper.updateList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量逻辑删除
     * @Date 2017/2/8 17:17
     */
    @Override
    public int deleteList(List<RoleMenuButton> list) {
        return roleMenuButtonMapper.deleteList(list);
    }

    @Override
    public List<MenuButton> queryBtnByLoginEmployeeVo(EmployeeVo employeeVo) throws Exception {
        List<MenuButton> roleMenuButtonList = roleMenuButtonMapper.queryBtnByLoginEmployeeVo(employeeVo);
        return roleMenuButtonList;
    }

    /**
     * @param menuId JSON菜单功能！
     */
    @Override
    public JSONObject queryBtnJsonObjectByMenu(String menuId, String roleId) throws Exception {
        JSONObject jsonObject = new JSONObject();
        Map param = new HashMap<>();
        param.put("menuId", menuId);
        param.put("roleId", roleId);
        List<MenuButton> roleMenuButtonList = roleMenuButtonMapper.queryBtnLstByMenu(param);
        jsonObject.put("total", roleMenuButtonList.size());
        jsonObject.put("rows", JSON.toJSON(roleMenuButtonList));
        return jsonObject;
    }

    /**
     * @param menuId 菜单ID
     * @param roleId 角色ID
     * @param btnIds 按钮Id
     */
    @Override
    public Integer insertBtnMenuRole(String menuId, String roleId, String btnIds) throws Exception {
        int resInt = 0;
        if (StringUtils.isNotEmpty(menuId) && StringUtils.isNotEmpty(roleId) && StringUtils.isNotEmpty(btnIds)) {
            // 删除所有的
            Map param = new HashMap<>();
            param.put("menuId", menuId);
            param.put("roleId", roleId);
            roleMenuButtonMapper.deleteBtnList(param);
            List<String> btnIdList = JSON.parseArray(btnIds, String.class);
            if (btnIdList != null && btnIdList.size() > 0) {
                for (String btnId : btnIdList) {
                    RoleMenuButton rmb = new RoleMenuButton();
                    rmb.setRoleMenuButtonId(UniqueUtil.uuid());
                    rmb.setMenuId(menuId);
                    rmb.setRoleId(roleId);
                    rmb.setButtonId(btnId);
                    resInt = resInt + roleMenuButtonMapper.insertSelective(rmb);
                }
            }
        }
        return resInt;
    }
}
